#include "widget.h"
#include "ui_widget.h"

#include <QTimer>
#include <QDebug>
#include <thread>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->progressBar->setMinimum(0);
    ui->progressBar->setMaximum(100);
    ui->progressBar->setValue(0);
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &Widget::handle);
    timer->start(100);

    // ui->lcdNumber->display(6000);
    // timer = new QTimer(this);
    // connect(timer, &QTimer::timeout, this, &Widget::handle);
    // timer->start(1000);

    // std::thread t([this](){
    //     int value = ui->lcdNumber->intValue();
    //     while(value)
    //     {
    //         value--;
    //         ui->lcdNumber->display(value);
    //         std::this_thread::sleep_for(std::chrono::seconds(1));
    //     }
    // });
}

Widget::~Widget()
{
    delete ui;
}

void Widget::handle()
{
    int value = ui->progressBar->value();
    if(value == 100)
    {
        ui->progressBar->setStyleSheet("QProgressBar::chunk {background-color: green;}");
        timer->stop();
        return;
    }
    ui->progressBar->setValue(value + 1);

    // int cnt = ui->lcdNumber->intValue();
    // cnt--;
    // ui->lcdNumber->display(cnt);
    // if(cnt == 0) timer->stop();
}
